//
//  PlaySoundYM.m
//  LazyCat
//
//  Created by jike on 16/4/11.
//  Copyright (c) 2016年 zhanshu. All rights reserved.
//

#import "PlaySoundYM.h"

@implementation PlaySoundYM
-(id)initForPlayingVibrate

{
    
    self = [super init];
    
    if (self) {
        
        soundID = kSystemSoundID_Vibrate;
        
    }
    
    return self;
    
}



-(id)initForPlayingSystemSoundEffectWith:(NSString *)resourceName ofType:(NSString *)type

{
    
    self = [super init];
    
    if (self) {
        
        NSString *path = [[NSBundle bundleWithIdentifier:@"com.apple.UIKit"] pathForResource:resourceName ofType:type];
        
        if (path) {
            
            SystemSoundID theSoundID;
            
            OSStatus error =  AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:path], &theSoundID);
            
            if (error == kAudioServicesNoError) {
                
                soundID = theSoundID;
                
            }else {
                
                NSLog(@"Failed to create sound ");
                
            }
            
        }
        
        
        
    }
    
    return self;
    
}



-(id)initForPlayingSoundEffectWith:(NSString *)filename Extension:(NSString *)extension

{
    
    self = [super init];
    
    if (self) {
        
        NSURL *fileURL = [[NSBundle mainBundle] URLForResource:filename withExtension:extension];
        
        if (fileURL != nil)
            
        {
            
            SystemSoundID theSoundID;
            
            OSStatus error = AudioServicesCreateSystemSoundID((__bridge CFURLRef)fileURL, &theSoundID);
            
            if (error == kAudioServicesNoError){
                
                soundID = theSoundID;
                
            }else {
                
                NSLog(@"Failed to create sound ");
                
            }
            
        }
        
    }
    
    return self;
    
}



-(void)play

{
    
    AudioServicesPlaySystemSound(soundID);
    
}



-(void)dealloc

{
    
    AudioServicesDisposeSystemSoundID(soundID);
    
}
@end
